﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_ListUsers : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aM in db.Manager
                select aM;

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            foreach (string s in search.Split(' '))
            {
                q = q.Where(aM=>aM.Name.Contains(s));
            }
        }
        #endregion

        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize();
        pds.DataSource = q.ToList();

        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        UsersView.DataSource = pds;
        UsersView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }

    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in UsersView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }

    protected void DeleteUser_Click(object sender, EventArgs e)
    {
        string mids = Request.Params["chkID"];
        if (mids != "")
        {
            CmsDataContext db = new CmsDataContext();
            string[] ids = mids.Split(',');

            try
            {
                #region 删除用户权限
                var q1 = from aP in db.Power
                         where ids.Contains(aP.ManagerID)
                         select aP;
                db.Power.DeleteAllOnSubmit(q1);
                #endregion

                #region 将登录日志设置为null
                var q2 = from aL in db.Logger
                         where ids.Contains(aL.ManagerID)
                         select aL;
                foreach (Logger l in q2)
                {
                    l.ManagerID = null;
                }
                #endregion

                #region 将文章的发布者设置为null
                var q3 = from aA in db.Article
                         where ids.Contains(aA.CreatorID)
                         select aA;
                foreach (Article a in q3)
                {
                    a.CreatorID = null;
                }

                q3 = from aA in db.Article
                     where ids.Contains(aA.VerifyID)
                     select aA;
                foreach (Article a in q3)
                {
                    a.VerifyID = null;
                }
                #endregion

                #region 删除用户信息
                var q4 = from aM in db.Manager
                         where ids.Contains(aM.ID)
                         select aM;
                db.Manager.DeleteAllOnSubmit(q4);
                #endregion

                //更新数据库
                db.SubmitChanges();

                MessageBox.Message = "所选择的用户已经成功删除。";
            }
            catch
            {
                MessageBox.Message = "删除用户信息时发生错误。";
            }

            BindData();
        }
    }
}